<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.CharArrayReader"%>
<%@page import="java.io.Reader"%>
<%@page import="oracle.sql.CLOB"%>
<%@page import="java.io.Writer"%>
<%@page import="java.sql.Clob"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String str = request.getParameter("content");
		String title = request.getParameter("title");
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
		String db_user = "semi";
		String db_pwd = "semi";
		Connection con; 
		PreparedStatement psmt;
		ResultSet rs;
		String sql1="insert into test(no,title,content) values(seq_bbs.nextval,?,empty_clob())";
		String sql2="select seq_bbs.currval from dual";
		String sql3="select content from test where no=? for update";
		int cnt1=0;
		try {
			Class.forName(driver).newInstance();
			con = DriverManager.getConnection(url, db_user, db_pwd);
			System.out.println("DB접속 성공");
			con.setAutoCommit(false);
			//일단 데이터를 저장한다. clob타입의 자리엔 empty_clob()를 이용하여 데이타를 넣을 공간을 확보한다.
			psmt = con.prepareStatement(sql1);
			psmt.setString(1, title);
			cnt1=psmt.executeUpdate();
			psmt.close();
			psmt = con.prepareStatement(sql2);
			rs = psmt.executeQuery();
			rs.next();
			int no = rs.getInt(1);
			//방금 insert한 데이타를 가져온다.
			psmt = con.prepareStatement(sql3);
			psmt.setInt(1, no);
			rs = psmt.executeQuery();
			if(rs.next()){
				//rs의 메소드 중 getClob를 통해 데이타를 가져온다.
				Clob clob= rs.getClob("content");
				Writer writer = ((CLOB)clob).getCharacterOutputStream();
				//clob에 넣을 문자를 char형의 배열로 만들어서 읽어들임
				Reader src = new CharArrayReader(str.toCharArray());
				char[] buffer = new char[1024];
				int read=0;
				while((read = src.read(buffer,0,1024))!=-1){
					writer.write(buffer,0,read);//clob에 데이타를 넣는역할인듯
				}
				src.close();
				writer.close();
			}
			con.commit();
			con.setAutoCommit(true);
			rs.close();
			psmt.close();
			System.out.println("입력끝");
			
			rs.close();
			psmt.close();
			con.close();
			%>
			<textarea rows="" cols=""><%=str %></textarea>
			<%
		} catch (Exception e) {
			e.printStackTrace();
		}
	%>
</body>
</html>